package cn.myspringboot.article.controller;

import cn.myspringboot.anno.PreAuthorize;
import cn.myspringboot.article.service.ArticleService;
import cn.myspringboot.user.domain.Article;
import cn.myspringboot.user.query.ArticleQuery;
import cn.myspringboot.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Api("文章的接口")
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @PreAuthorize(name = "添加或修改文章信息",sn = "article:add",descs ="添加或修改文章信息")
    @ApiOperation("添加或修改文章信息")
    @PostMapping
    public R add(@RequestBody Article article) {
        if (article.getArticleType() == null || "".equals(article.getArticleType())) {
            return R.fail("请输入文章类型");
        }
        if(article.getArticleRemark() == null || "".equals(article.getArticleRemark())) {
            return R.fail("请输入文章简介");
        }
        if(article.getArticleContent() == null || "".equals(article.getArticleContent())) {
            return R.fail("请输入文章内容");
        }
        if(article.getArticleName() == null || "".equals(article.getArticleName())) {
            return R.fail("请输入文章名字");
        }
        if(article.getId() == null){
            article.setCreateTime(new Date());
            article.setUpdateTime(new Date());
            article.setArticleCommentNum(0);
            article.setArticleReadCount(0);
            article.setArticleStarNum(0);
            //todo
            articleService.addArticle(article);
        }else {
            article.setUpdateTime(new Date());
            articleService.updateArticle(article);
        }
        return R.ok();
    }

    @PreAuthorize(name = "根据id删除文章信息",sn = "article:delete",descs ="根据id删除文章信息")
    @ApiOperation("根据id删除文章信息")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id) {
        articleService.deleteArticle(id);
        return R.ok();
    }

    @PreAuthorize(name = "批量删除文章信息",sn = "article:delBatch",descs ="批量删除文章信息")
    @ApiOperation("批量删除文章信息")
    @PostMapping("/delBatch")
    public R delBatch(@RequestBody List<Long> ids) {
        articleService.batchdelArticle(ids);
        return R.ok();
    }

    @PreAuthorize(name = "根据id查询文章信息",sn = "article:getById",descs ="根据id查询文章信息")
    @ApiOperation("根据id查询文章信息")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(articleService.getArticleById(id));
    }

    @PreAuthorize(name = "查询所有文章信息",sn = "article:getAll",descs ="查询所有文章信息")
    @ApiOperation("查询所有文章信息")
    @GetMapping("/getAll")
    public R getAll() {
        return R.ok(articleService.getArticleAll());
    }

    @PreAuthorize(name = "分页查询文章信息",sn = "article:page",descs ="分页查询文章信息")
    @ApiOperation("分页查询文章信息")
    @PostMapping("/page")
    public R page(@RequestBody ArticleQuery articleQuery) {
        return R.ok(articleService.getArticleByPage(articleQuery));
    }

    @PreAuthorize(name = "查询首页需要的文章信息",sn = "article:indexPage",descs ="查询首页需要的文章信息")
    @ApiOperation("查询首页需要的文章信息")
    @PostMapping("/indexPage")
    public R indexPage(@RequestBody ArticleQuery articleQuery) {
        return R.ok(articleService.getPage(articleQuery));
    }

    @PreAuthorize(name = "获取文章点赞数",sn = "article:star",descs ="获取文章点赞数")
    @ApiOperation("获取文章点赞数")
    @GetMapping("/star/{id}")
    public R star(HttpServletRequest request,@PathVariable("id") Long id) {
        Integer star = articleService.star(request, id);
        return R.ok(star);
    }

    @PreAuthorize(name = "获取文章阅读量",sn = "article:read",descs ="获取文章阅读量")
    @ApiOperation("获取文章阅读量")
    @GetMapping("/read/{id}")
    public R read(HttpServletRequest request,@PathVariable("id") Long id) {
        Integer read = articleService.read(request, id);
        return R.ok(read);
    }

    @PreAuthorize(name = "获取文章类型",sn = "article:getByType",descs ="获取文章类型")
    @ApiOperation("获取文章类型")
    @PostMapping ("/getByType")
    public R getByType() {
        return R.ok(articleService.findByTypeName());
    }

    @PreAuthorize(name = "获取文章时间",sn = "article:getArticleYear",descs ="获取文章时间")
    @ApiOperation("获取文章时间")
    @GetMapping("/getArticleYear")
    public R getArticleYear() {
        return R.ok(articleService.getArticleYear());
    }

    @PreAuthorize(name = "获取文章年月",sn = "article:getByYearAndMonth",descs ="获取文章年月")
    @ApiOperation("获取文章年月")
    @GetMapping("/getByYearAndMonth")
    public R getByYearAndMonth(){
        return R.ok(articleService.findByYearAndMonth());
    }

    @PreAuthorize(name = "获取文章名称和点赞数",sn = "article:fingStarsAndNameByArticle",descs ="获取文章名称和点赞数")
    @ApiOperation("获取文章名称和点赞数")
    @GetMapping("/fingStarsAndNameByArticle")
    public R fingStarsAndNameByArticle(){
        return R.ok(articleService.fingStarsAndNameByArticle());
    }
}
